<?php

namespace app\api\controller;

use app\common\business\User as userBn;
use app\common\business\Guess as guessBn;
use think\Validate;
use app\common\redis\db0\Userinfo as redisBn;

class Guess extends \app\common\controller\HomeBase {

    /**
     * 世界杯球队
     */
    public function team() {
        try {
            $params = input();
            $data = guessBn::team($params);
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加推演
     * guess_16='法国;中国;dsfdf'
     * guess_8='dsfsdf;sdfsdfs'
     * guess_4='dsfsdf;dsfsdf'
     * guess_1='sdfsd;sdfsd'
     * guess_3='sdfsd;sdfsd'
     * guess_first='sdfsd'
     * guess_three="ddd"
     * 
     */
    public function add() {
        try {
            $user_id = $this->_validLogin();
            $params = input();
            $validate = Validate('Guess');
            $params = $validate->scene('add')->check($params);
            if (!$params) {
                exception($validate->getError(), 10000003);
            }
            $params['guess_type'] = 32;
            $params['user_id'] = $user_id;
            $data = guessBn::addguess($params);
            $this->_echoSuccessMessage('推演冠军成功', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 记录手机号
     */
    public function savemobile() {
        try {
            $user_id = $this->_validLogin();
            $params = input();
            $validate = Validate('Guess');
            $params = $validate->scene('savemobile')->check($params);
            if (!$params) {
                exception($validate->getError(), 10000003);
            }
            $params['user_id'] = $user_id;
            $data = userBn::savemobile($params);
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 积分排行榜
     */
    public function pointlist() {
        try {
            $user_id = $this->_isLogin();
            $params = input();
            $validate = Validate('Guess');
            $params = $validate->scene('pointlist')->check($params);
            if ($validate->getError()) {
                exception($validate->getError(), 10000003);
            }
            $params["need_total"] = 1;
            $params["count"] = 99;
            $data = userBn::getPointRank($params);
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 我的积分排名情况
     */
    public function mypointranking() {
        try {
            $userinfo = $this->_validLogininfo();
            $data = userBn::mypointranking($userinfo);
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 积分排行榜
     */
    public function firstlist() {
        try {
            $user_id = $this->_isLogin();
            $params = input();
            $validate = Validate('Guess');
            $params = $validate->scene('pointlist')->check($params);
            if ($validate->getError()) {
                exception($validate->getError(), 10000003);
            }
            $params["need_total"] = 1;
            $data = guessBn::firstlist($params, $user_id);
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 积分排行榜
     */
    public function test() {
        try {

            $data = userBn::creatPointRank();
            $this->_echoSuccessMessage('', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分享复活一次
     */
    public function sharesetcount() {
        try {
            $user_id = $this->_isLogin();
            if ($user_id > 0) {
                $redis = new redisBn();
                $redis->set("sharegetcount:" . $user_id, 1);
            }
            $this->_echoSuccessMessage('', 1);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分享复活一次
     */
    public function sharegetcount() {
        try {
            $user_id = $this->_isLogin();
            $count = 0;
            if ($user_id > 0) {
                $redis = new redisBn();
                $count = $redis->get("sharegetcount:" . $user_id);
            }
            $this->_echoSuccessMessage('', $count == 1 ? 1 : 0);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 已复活清空记录
     */
    public function sharedelcount() {
        try {
            $user_id = $this->_isLogin();
            $count = 0;
            if ($user_id > 0) {
                $redis = new redisBn();
                $count = $redis->set("sharegetcount:" . $user_id, 0);
            }
            $this->_echoSuccessMessage('', $count);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }

}
